$(function () {
    $("#jqGrid").Grid({
        url: '../sportscreditrule/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '规则编码', name: 'keycode', index: 'keycode', width: 80},
            {label: '规则名称', name: 'name', index: 'name', width: 80},
            {label: '大类', name: 'category', index: 'category', width: 80,
                formatter: function (value) {
                    if(value==0)
                        return '邀请任务';
                    else if(value==1)
                        return '每日任务';
                    else
                        return '';
                }},
            {
                label: '跳转类型', name: 'type', index: 'type', width: 120,
                formatter: function (value) {
                    if(value==0)
                        return 'APP首页';
                    else if(value==1)
                        return '探索首页';
                    else if(value==2)
                        return '街区首页';
                    else if(value==3)
                        return '我的首页';
                    else
                        return '';
                  /*  if(value==0)
                        return '邀请好友';
                    else if(value==1)
                        return '分享到朋友圈';
                    else if(value==2)
                        return '每日签到';
                    else if(value==3)
							return '跑步/行走/骑行10公里';
                    else if(value==4)
                        return '发帖';
                    else if(value==5)
                        return '评论';
                    else
                        return '';*/
                }
            },
			{label: '获取积分', name: 'creditcount', index: 'creditcount', width: 80},
			{label: '状态', name: 'enabled', index: 'enabled', width: 80, formatter: function (value) {
                    return value === 0 ?
                        '<span class="label label-danger">禁用</span>' :
                        '<span class="label label-success">正常</span>';
                }},
			{label: '限制次数', name: 'limitnum', index: 'limitnum', width: 80},
            {label: '创建日期', name: 'createdat', index: 'createdAt', width: 80, formatter: function (value) {
                    return transDate(value);
                }}]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		sportsCreditRule: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.sportsCreditRule = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.sportsCreditRule.id == null ? "../sportscreditrule/save" : "../sportscreditrule/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.sportsCreditRule),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../sportscreditrule/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../sportscreditrule/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.sportsCreditRule = r.sportsCreditRule;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});